package com.jsyxdl.primary;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.container.model.dto.search.ContainerEntityParamDTO;
import com.hustcad.plm.pdm.container.model.vo.search.ContainerEntityVO;
import com.hustcad.plm.pdm.container.service.TyplmPdmContainerService;
import com.hustcad.plm.pdm.folder.controller.FolderController;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderVO;
import com.hustcad.plm.pdm.folder.model.vo.SubfolderWebVO;
import com.hustcad.plm.pdm.folder.service.TyplmSubFolderLinkService;
import com.hustcad.plm.pdm.folder.service.impl.TyplmSubFolderLinkServiceImpl;
import com.hustcad.plm.pdm.partbom.dto.TyPartAndMasterDTO;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.jsyxdl.emuns.MyCollectErrorCodeEnum;
import com.jsyxdl.primary.service.PartTypeDefService;
import com.jsyxdl.utils.PropertiesMapperUtils;
import com.ty.basic.entity.part.PartDO;
import com.ty.basic.entity.type.TypeDO;
import com.ty.basic.response.ResponseResult;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChangesPartFolder implements PartTypeDefService {

    /**
     * 适合新建零部件，新增BOM行
     *
     * @param partDO
     */
    @Override
    public void insertPartBefore(PartDO partDO) {
        TyplmTypeService typlmTypeService = SpringUtil.getBean(TyplmTypeService.class);

        JSONObject mapper = PropertiesMapperUtils.getPropertiesMapper("system.storage.location");
        if (mapper == null) {
            return;
        }


        TypeDO typeDO = typlmTypeService.queryTypeByOID(partDO.getTypeoid());

        String typeDefName = typeDO.getName();
        if (mapper.containsKey(typeDefName) == false) {
            return;
        }
        JSONObject mapperObj = mapper.getJSONObject(typeDefName);
        if (mapperObj == null) {
            return;
        }
        ContainerEntityParamDTO param = new ContainerEntityParamDTO();
        param.setName(mapperObj.getString("库名称"));
        String libraryType = mapperObj.getString("库类型");
        if ("存储库".equals(libraryType)) {
            param.setOtype("ty.inteplm.library.CTyLibrary");
        } else if ("产品库".equals(libraryType)) {
            param.setOtype("ty.inteplm.product.CTyPDMLinkProduct");
        } else if ("项目库".equals(libraryType)) {
            param.setOtype("ty.inteplm.type.CTyTypeDef");
        }
        TyplmPdmContainerService typlmPdmContainerService = SpringUtil.getBean(TyplmPdmContainerService.class);
        List<ContainerEntityVO> containerEntityVOList = typlmPdmContainerService.queryAllContainerListMyself(param);
        if (containerEntityVOList == null || containerEntityVOList.size() == 0) {
            return;
        }

        BigInteger oid = containerEntityVOList.get(0).getDefaultcabinetoid();
        String otype = containerEntityVOList.get(0).getDefaultcabinetotype();

        SubfolderWebVO partenSubfolderWebVO = getStorageLocationSubfolder(oid, otype,
                                                                          mapperObj.getString("文件夹路径"));


        if (partenSubfolderWebVO == null) {
            throw MyCollectErrorCodeEnum.vue.getException(
                    "请在【" + mapperObj.getString("库名称") + "】库中创建【" + mapperObj.getString(
                            "文件夹路径") + "】文件夹");
        }

        if (partenSubfolderWebVO != null) {
            partDO.setSubfolderoid(new BigInteger(partenSubfolderWebVO.getOid()));
            partDO.setSubfolderotype(partenSubfolderWebVO.getOtype());
        }
    }

    /**
     * 新建下游
     *
     * @param partDOList
     */
    @Override
    public void insertPartBefore(List<PartDO> partDOList) {
        TyplmTypeService typlmTypeService = SpringUtil.getBean(TyplmTypeService.class);
        JSONObject mapper = PropertiesMapperUtils.getPropertiesMapper("system.storage.location");

        for (int a = 0; a < partDOList.size(); a++) {
            PartDO partDO = partDOList.get(a);

            TypeDO typeDO = typlmTypeService.queryTypeByOID(partDO.getTypeoid());

            String typeDefName = typeDO.getName();
            if (mapper.containsKey(typeDefName) == false) {
                return;
            }
            JSONObject mapperObj = mapper.getJSONObject(typeDefName);
            if (mapperObj == null) {
                return;
            }
            ContainerEntityParamDTO param = new ContainerEntityParamDTO();
            param.setName(mapperObj.getString("库名称"));
            String libraryType = mapperObj.getString("库类型");
            if ("存储库".equals(libraryType)) {
                param.setOtype("ty.inteplm.library.CTyLibrary");
            } else if ("产品库".equals(libraryType)) {
                param.setOtype("ty.inteplm.product.CTyPDMLinkProduct");
            } else if ("项目库".equals(libraryType)) {
                param.setOtype("ty.inteplm.type.CTyTypeDef");
            }
            TyplmPdmContainerService typlmPdmContainerService = SpringUtil.getBean(TyplmPdmContainerService.class);
            List<ContainerEntityVO> containerEntityVOList = typlmPdmContainerService.queryAllContainerListMyself(param);
            if (containerEntityVOList == null || containerEntityVOList.size() == 0) {
                return;
            }

            BigInteger oid = containerEntityVOList.get(0).getDefaultcabinetoid();
            String otype = containerEntityVOList.get(0).getDefaultcabinetotype();

            SubfolderWebVO partenSubfolderWebVO = getStorageLocationSubfolder(oid, otype,
                                                                              mapperObj.getString("文件夹路径"));


            if (partenSubfolderWebVO == null) {
                return;
            }

            if (partenSubfolderWebVO != null) {
                partDO.setSubfolderoid(new BigInteger(partenSubfolderWebVO.getOid()));
                partDO.setSubfolderotype(partenSubfolderWebVO.getOtype());
            }
        }


    }


    public static String getStorageLocation(JSONObject item, String typeDefName) {
        String dir = null;
        for (Map.Entry<String, Object> entity : item.getInnerMap().entrySet()) {
            Object vlaue = entity.getValue();
            if (vlaue instanceof JSONObject) {
                JSONObject valueObj = (JSONObject) vlaue;
                dir = getStorageLocation(valueObj, typeDefName);
            } else {
                if (typeDefName.equals(entity.getKey())) {
                    dir = (String) entity.getValue();
                }
            }
            if (dir != null) {
                return dir;
            }
        }
        return dir;
    }

    public static SubfolderWebVO getStorageLocationSubfolder(BigInteger oid, String otype, String mapperKey) {
        TyplmSubFolderLinkServiceImpl typlmSubFolderLinkServiceImpl = SpringUtil.getBean(
                TyplmSubFolderLinkServiceImpl.class);

        List<SubfolderWebVO> subfolderWebVOList = typlmSubFolderLinkServiceImpl.querySubFolderListByParent(oid, otype);

        SubfolderWebVO partenSubfolderWebVO = null;

        for (int i = 0; i < subfolderWebVOList.size(); i++) {
            if (mapperKey.equals(subfolderWebVOList.get(i).getName())) {
                partenSubfolderWebVO = subfolderWebVOList.get(i);
                break;
            } else {
                if ("true".equals(subfolderWebVOList.get(i).getHasChildren())) {
                    partenSubfolderWebVO = getStorageLocationSubfolder(
                            new BigInteger(subfolderWebVOList.get(i).getOid()), subfolderWebVOList.get(i).getOtype(),
                            mapperKey);
                }
            }
            if (partenSubfolderWebVO != null) {
                return partenSubfolderWebVO;
            }
        }
        return partenSubfolderWebVO;
    }

}
